package mediadisplay.viewerarea 
{
	import assets.FontAssets;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	/**
	 * ...
	 * @author Anders Figenschow
	 */
	public class ScreenText extends Sprite
	{
		public var tf:TextField;
		private var textformat:TextFormat;
		private var _txt:String;
		public var _rule:Rule;
		public static const DONE:String = "done";
		public var onScreen:Boolean = false;
		private var editFrame:EditFrame;
		private var _index:int;
		public var _viewerArea:ViewerArea;
		public var _xpos:Number;
		public var _ypos:Number;
		public var _zpos:Number;
		public var _rotX:Number;
		public var _rotY:Number;
		public var _rotZ:Number;
		public var _scale:Number;
		public var w:Number;
		public var h:Number;
		public static var HEY_IM_WORKING_HERE:Boolean = false;
		
		public var assetImage:Loader;
		private var request:URLRequest;
		private var _imageURL:String;
		private var _color:uint;
		public var interactionTarget:Number;
		public var _ignore:Boolean;
		public var _background:String;
		public var keepFullAlpha:Boolean = false;
		
		public function ScreenText(viewerArea:ViewerArea, index:int, txt:String, rule:Rule, xpos:Number, ypos:Number, zpos:Number, rotX:Number, rotY:Number, rotZ:Number, scale:Number, imageURL:String = "", background:String = "", color:uint = 0xffffff, ignore:Boolean = false, interactionTarget:Number = -1) 
		{
			this.interactionTarget = interactionTarget;
			_ignore = ignore;
			_color = color;
			_background = background;
			_imageURL = imageURL;
			_index = index;
			_viewerArea = viewerArea;
			_scale = scale;
			_rotX = rotX;
			_rotY = rotY;
			_rotZ = rotZ;
			_zpos = zpos;
			_ypos = ypos;
			_xpos = xpos;
			_rule = rule;
			_txt = txt;
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			textformat = new TextFormat();
			textformat.color = _color;
			textformat.size = 40;
			textformat.font = FontAssets.FLYER_LT_STD;
			textformat.align = TextFormatAlign.CENTER;
			
			tf = new TextField();
			addChild(tf);
			tf.defaultTextFormat = textformat;
			tf.autoSize = TextFieldAutoSize.CENTER;
			tf.text = _txt;
			tf.embedFonts = true;
			tf.x = -tf.width / 2;
			tf.y = -tf.height / 2;
			tf.selectable = false;
			tf.mouseEnabled = false;
			tf.visible = true;
			
			// if Rule:
			// TODO fix rules
			//x = _rule._startX;
			//y = _rule._startY;
			//z = _rule._startZ;
			//scaleX = scaleY = _rule._startScale;
			
			x = _xpos;
			y = _ypos;
			z = _zpos;
			rotationZ = _rotZ;
			rotationX = _rotX;
			rotationY = _rotY;
			scaleX = scaleY = _scale;
			
			visible = false;
			//mouseEnabled = false;
			mouseChildren = false;
			
			editFrame = new EditFrame(this);
			addChild(editFrame);
			editFrame.visible = false;
			
			addEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			addEventListener(Event.ENTER_FRAME, enterframe);
			addEventListener(MouseEvent.CLICK, clickHandler);
			w = width;
			h = height;
			
			if (_imageURL != "") loadImage();
			if (_txt.indexOf(".jpg") > -1 || _txt.indexOf(".png") > -1) {
				_imageURL = _txt;
				tf.visible = false;
				keepFullAlpha = true;
				alpha = 1;
				loadImage();
			}
		}
		
		private function clickHandler(e:MouseEvent):void 
		{
			return;
			if (interactionTarget == 0) return;
			trace("Interaction on element.  Target is : " + interactionTarget);
			_viewerArea._main.soundgraph.gotoPosition(interactionTarget * 1000);
		}
		
		public function setColor(color:uint):void {
			var newTextformat:TextFormat = new TextFormat();
			newTextformat.color = color;
			newTextformat.size = 40;
			newTextformat.font = FontAssets.FLYER_LT_STD;
			newTextformat.align = TextFormatAlign.CENTER;
			tf.setTextFormat(newTextformat);
			tf.defaultTextFormat = newTextformat;
			_color = color;
		}
		
		private function loadImage():void 
		{
			trace("i found an image: " + _imageURL);
			request = new URLRequest(_imageURL);
			assetImage = new Loader();
			assetImage.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
			assetImage.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			assetImage.load(request);
		}
		
		private function imageLoaded(e:Event):void 
		{
			addChild(assetImage);
			assetImage.x = -assetImage.width / 2;
			assetImage.y = -assetImage.height / 2;
			assetImage.contentLoaderInfo.removeEventListener(Event.COMPLETE, imageLoaded);
			assetImage.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			addChild(editFrame);
			tf.width = assetImage.width;
			tf.height = assetImage.height;
			editFrame.redraw();
		}
		
		private function ioErrorHandler(e:IOErrorEvent):void 
		{
			trace("image load error: " + e);
		}
		
		private function enterframe(e:Event):void 
		{
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].scale = scaleX;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].x = x;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].y = y;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].z = z;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].rotationX = rotationX;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].rotationY = rotationY;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].rotationZ = rotationZ;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].content = tf.text;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].color = _color;
			_viewerArea._main.soundgraph.timelineContent.keypoints[_index].ignore = _ignore;
		}
		
		public function editToggle(show:Boolean):void {
			if (show) {
				editFrame.visible = true;
				mouseEnabled = true;
				mouseChildren = true;
				editFrame.redraw();
			} else {
				editFrame.visible = false;
				//mouseEnabled = false;
				mouseChildren = false;
			}
		}
		
		public function setEditMode(mode:Boolean = true):void {
			if (mode) {
				mouseEnabled = true;
				mouseChildren = true;
			} else {
				mouseEnabled = false;
				mouseChildren = false;
			}
		}
		
		public function resize(startMousePosition:Point, corner:int):void {
			var newWidth:Number;
			var newHeight:Number;
			
			if (corner == 0) {
				var diffX:Number = startMousePosition.x - stage.mouseX;
				width = w + (diffX * 2);
				scaleY = scaleX;
				var currentObjectIndex:int;
				//currentObjectIndex = _viewerArea._main.soundgraph.timelineContent._currentFocusedMarker.index;
				currentObjectIndex = _index;
				_viewerArea._main.soundgraph.timelineContent.keypoints[currentObjectIndex].scale = scaleX;
			}
			rotation = _rotZ;
		}
		
		private function cleanup(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, cleanup);
			removeEventListener(Event.ENTER_FRAME, enterframe);
			if (_imageURL != "") {
				removeChild(assetImage);
				assetImage.unloadAndStop();
			}
			removeChild(tf);
			removeChild(editFrame);
		}
		public function setText(txt:String):void {
			if (HEY_IM_WORKING_HERE) {
				return;
			}
			if (txt == "" || tf.text == txt || tf.text.indexOf(".png") > -1 || tf.text.indexOf(".jpg") > -1) return; // TODO this is a hack to stop the first tf to be deleted for no apparent reason.
			tf.text = _txt = txt;
			w = width;
			h = height;
			editFrame.redraw();
			trace("text set to [" + txt + "]");
		}
		
	}

}